package com.sc.nft.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dao.CollectionConsensusDao;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.UserConsensusLogDao;
import com.sc.nft.entity.CollectionConsensus;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.dto.DigitalcollectionByUserCreatorIdDTO;
import com.sc.nft.entity.vo.EcologyCollectionVO;
import com.sc.nft.entity.vo.HomePageDigitalCollectionProductVO;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.DigitalCollectionService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/digitalCollection")
@Api(tags = "藏品数据")
public class DigitalCollectionController {

    private final DigitalCollectionService digitalCollectionService;
    private final DigitalCollectionDao digitalCollectionDao;
    private final CollectionConsensusDao collectionConsensusDao;
    private final UserConsensusLogDao userConsensusLogDao;


    @ApiOperation(value = "dao主页中的藏品信息", notes = "dao主页中的藏品信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/digitalcollectionByUserCreatorId", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<HomePageDigitalCollectionProductVO> digitalCollectionByUserCreatorId(@RequestBody DigitalcollectionByUserCreatorIdDTO digitalcollectionByUserCreatorIdDTO) {
        return PageResult.success(digitalCollectionService.digitalcollectionByUserCreatorId(digitalcollectionByUserCreatorIdDTO));
    }

    @ApiOperation(value = "生态藏品列表", notes = "生态藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/ecologyCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<EcologyCollectionVO>> ecologyCollection() {
        List<EcologyCollectionVO> result = Lists.newArrayList();
        List<DigitalCollection> ecologyCollectionSimpleList = digitalCollectionDao.getEcologyCollectionSimpleList();
        ecologyCollectionSimpleList.forEach(list->{
            EcologyCollectionVO ecologyCollectionVO = new EcologyCollectionVO();
            ecologyCollectionVO.setId(list.getId());
            ecologyCollectionVO.setFirstTitle(list.getFirstTitle());
            ecologyCollectionVO.setCreationContent(list.getCreationContent());
            CollectionConsensus collectionConsensus = collectionConsensusDao.getByCollectionId(list.getId());
            if(ObjectUtil.isNull(collectionConsensus)){
                ecologyCollectionVO.setTodayLastChangeNum(BigDecimal.ZERO.toPlainString());
            }else{
                ecologyCollectionVO.setTodayLastChangeNum(NumberUtil.max(BigDecimal.ZERO,collectionConsensus.getConsensusNum().subtract(userConsensusLogDao.getTodaySumChange(UserPool.getUser().getId(), list.getId()))).toPlainString());
            }
            result.add(ecologyCollectionVO);
        });
        return Result.success(result);
    }

    @ApiOperation(value = "dao主页中的藏品信息IOS使用", notes = "dao主页中的藏品信息IOS使用", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/digitalcollectionByUserCreatorIdIos", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<HomePageDigitalCollectionProductVO> digitalCollectionByUserCreatorIdIos(@RequestBody DigitalcollectionByUserCreatorIdDTO digitalcollectionByUserCreatorIdDTO) {
        return PageResult.success(digitalCollectionService.digitalcollectionByUserCreatorId(digitalcollectionByUserCreatorIdDTO));
    }


    //@ApiOperation(value = "dao主页中的盲盒列表", notes = "dao主页中的盲盒列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    //@PostMapping(value = "/boxCollectionByUserCreatorId", produces = MediaType.APPLICATION_JSON_VALUE)
    //public PageResult<BoxCollectionByUserCreatorIdVO> boxCollectionByUserCreatorId(@RequestBody DigitalcollectionByUserCreatorIdDTO request) {
    //    return PageResult.success(digitalCollectionService.boxCollectionByUserCreatorId(request.getUserCreatorId(),request.getPageNo(),request.getPageSize()));
    //}
}
